package org.example.order.controller;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.example.order.VO.AddShippingAddressVO;
import org.example.order.VO.UpdateShippingAddressVO;
import org.example.order.common.APIResponse;
import org.example.order.common.ResponseCode;
import org.example.order.entity.ShippingAddress;
import org.example.order.service.ShippingAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/api/shipping-address")
@Slf4j
public class ShippingAddressController {

    @Resource
    private ShippingAddressService shippingAddressService;

    /**
     * 添加收货地址
     */
    @PostMapping("add")
    public APIResponse<ShippingAddress> add(@RequestBody @Valid AddShippingAddressVO addressVO) {
        ShippingAddress shippingAddress = new ShippingAddress();
        BeanUtils.copyProperties(addressVO, shippingAddress);

        ShippingAddress result = shippingAddressService.add(shippingAddress);
        return new APIResponse<>(ResponseCode.SUCCESS, "添加成功", result);
    }

    /**
     * 删除收货地址
     */
    @DeleteMapping("delete/{id}")
    public APIResponse<Boolean> delete(@PathVariable Long id, @RequestParam Long userId) {
        boolean result = shippingAddressService.deleteById(id, userId);
        return new APIResponse<>(result ? ResponseCode.SUCCESS : ResponseCode.FAIL,
                result ? "删除成功" : "删除失败", result);
    }

    /**
     * 更新收货地址
     */
    @PutMapping("update")
    public APIResponse<Boolean> update(@RequestBody UpdateShippingAddressVO addressVO) {
        // 获取原地址信息
        ShippingAddress originalAddress = shippingAddressService.getById(addressVO.getId());
        if (originalAddress == null) {
            return new APIResponse<>(ResponseCode.FAIL, "地址不存在", false);
        }

        // 创建要更新的地址对象
        ShippingAddress shippingAddress = new ShippingAddress();
        BeanUtils.copyProperties(addressVO, shippingAddress);

        // 检查VO中isDefault是否为null (表示未传递该字段)
        if (addressVO.getIsDefault() == null) {
            // 未传递isDefault，使用原来的值
            shippingAddress.setIsDefault(originalAddress.getIsDefault());
        }
        // 否则使用传递的新值

        boolean result = shippingAddressService.updateAddress(shippingAddress);
        return new APIResponse<>(result ? ResponseCode.SUCCESS : ResponseCode.FAIL,
                result ? "更新成功" : "更新失败", result);
    }

    /**
     * 设为默认地址
     */
    @PutMapping("/default/{id}")
    public APIResponse<Boolean> setDefault(@PathVariable Long id, @RequestParam Long userId) {
        boolean result = shippingAddressService.setDefault(id, userId);
        return new APIResponse<>(result ? ResponseCode.SUCCESS : ResponseCode.FAIL,
                result ? "设置成功" : "设置失败", result);
    }

    /**
     * 获取用户所有收货地址
     */
    @GetMapping("/list")
    public APIResponse<List<ShippingAddress>> list(@RequestParam Long userId) {
        List<ShippingAddress> addressList = shippingAddressService.listByUserId(userId);
        return new APIResponse<>(ResponseCode.SUCCESS, "查询成功", addressList);
    }

    /**
     * 获取用户默认收货地址
     */
    @GetMapping("/default")
    public APIResponse<ShippingAddress> getDefaultAddress(@RequestParam Long userId) {
        ShippingAddress address = shippingAddressService.getDefaultAddress(userId);
        return new APIResponse<>(ResponseCode.SUCCESS, "查询成功", address);
    }

    /**
     * 根据ID查询收货地址
     */
    @GetMapping("/{id}")
    public APIResponse<ShippingAddress> getById(@PathVariable Long id) {
        ShippingAddress address = shippingAddressService.getById(id);
        return new APIResponse<>(address != null ? ResponseCode.SUCCESS : ResponseCode.FAIL,
                address != null ? "查询成功" : "地址不存在", address);
    }
}